package com.example.glkotlin

import android.annotation.SuppressLint
import android.content.Intent
import android.net.Uri
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.TextView
import android.widget.Toast
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.glkotlin.adapter.FruitAdapter
import com.example.glkotlin.bean.FruitBean
import java.util.ArrayList
import java.util.Objects

class HomeActivity : AppCompatActivity() {

    private var mFruitListView : RecyclerView? = null
    private var mFruitDataList : ArrayList<FruitBean> = ArrayList<FruitBean>()

    @SuppressLint("MissingInflatedId")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_home)
        val textView = findViewById<TextView>(R.id.tips)
        val data = intent.getStringExtra("data")
        Toast.makeText(this, data, Toast.LENGTH_SHORT).show()
        textView.setOnClickListener {
            // 指定action 隐式启动
//            val intent = Intent(Intent.ACTION_VIEW)
//            intent.data = Uri.parse("https://www.baidu.com")
//            startActivity(intent)
            val intent = Intent()
            intent.putExtra("data_return", "Nice to meet you too!")
            setResult(RESULT_OK, intent)
            finish()
        }

        mFruitListView = findViewById(R.id.data_list)
        initFruitData()
        mFruitListView?.let {
            it.adapter = FruitAdapter(mFruitDataList)
            it.layoutManager = LinearLayoutManager(this)
        }

    }

    private fun initFruitData() {
        // repeat(n:int){} 这个函数代表 后面表达式的内容重复执行n遍
        repeat(2) {
            mFruitDataList.add(FruitBean("Apple", R.drawable.ic_launcher_background))
            mFruitDataList.add(FruitBean("Banana", R.drawable.ic_launcher_background))
            mFruitDataList.add(FruitBean("Orange", R.drawable.ic_launcher_background))
            mFruitDataList.add(FruitBean("Watermelon", R.drawable.ic_launcher_background))
            mFruitDataList.add(FruitBean("Pear", R.drawable.ic_launcher_background))
            mFruitDataList.add(FruitBean("Cherry", R.drawable.ic_launcher_background))
        }
    }
}